<html>
<head>
    <script src="../OLLoader.js"></script>
    <script type="text/javascript">

    function test_initialize(t) {
        t.plan(6);
        var layer = "foo";
        var control = new OpenLayers.Control.TransformFeature(layer);
        
        t.ok(control.layer == layer,
             "constructor sets layer correctly");
        t.ok(control.dragControl instanceof OpenLayers.Control.DragFeature,
             "constructor sets the drag control correctly");
        t.ok(control.box instanceof OpenLayers.Feature.Vector,
             "box feature created");
        t.eq(control.handles.length, 8, "8 handles created");
        t.eq(control.rotationHandles.length, 4, "4 rotation handles created")
        t.eq(typeof control.rotationHandleSymbolizer, "object",
            "rotationHandleSymbolizer initialized");
        control.destroy();
    }

    function test_destroy(t) {
        t.plan(1);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.TransformFeature(layer);
        control.dragControl.destroy = function() {
            t.ok(true,
                 "control.destroy calls destroy on drag control");
        };
        control.destroy();
        map.destroy();
    }
    
    function test_activate(t) {
        t.plan(3);
        var map = new OpenLayers.Map("map");
        var layer = new OpenLayers.Layer.Vector();
        map.addLayer(layer);
        var control = new OpenLayers.Control.TransformFeature(layer);
        map.addControl(control);
        
        t.ok(!control.dragControl.active,
             "drag control is not active prior to activating control");
        control.activate();
        t.ok(control.dragControl.active,
             "drag control is active after activating control");
        t.ok(control.box.layer === layer, "box added to layer");

        map.destroy();
    }
    
    function test_setFeature(t) {
        t.plan(6);
        var map = new OpenLayers.Map("map", {allOverlays: true});
        var layer = new OpenLayers.Layer.Vector();
        var feature = new OpenLayers.Feature.Vector(
            OpenLayers.Geometry.fromWKT("POLYGON((-1 -1, 1 -1, 1 1, -1 1))"));
        layer.addFeatures([feature]);
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0, 0), 18);
        var control = new OpenLayers.Control.TransformFeature(layer);
        map.addControl(control);
        var log = [];
        control.events.on({
            "beforesetfeature": function(e) { log.push(e); },
            "setfeature": function(e) { log.push(e); }
        });
        control.setFeature(feature);
        
        t.eq(log[0].type, "beforesetfeature", "beforesetfeature event fired with correct event type");
        t.eq(log[1].type, "setfeature", "setfeature event fired with correct event type");
        
        t.ok(control.active, "control activated on setFeature");
        t.ok(feature.geometry.getBounds().equals(control.box.geometry.getBounds()), "box positioned correctly");
        t.geom_eq(control.handles[0].geometry, control.box.geometry.components[0], "handle positioned with box");
        
        var center = new OpenLayers.LonLat(1, 1);
        control.box.move(center);
        t.geom_eq(control.handles[0].geometry, control.box.geometry.components[0], "handle moved with box");
    }
    
    function test_handleMove(t) {
        t.plan(16);
        var map = new OpenLayers.Map("map", {allOverlays: true});
        var layer = new OpenLayers.Layer.Vector();
        var feature = new OpenLayers.Feature.Vector(
            OpenLayers.Geometry.fromWKT("POLYGON((-1 -1, 1 -1, 1 1, -1 1))"));
        layer.addFeatures([feature]);
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0, 0), 18);
        var control = new OpenLayers.Control.TransformFeature(layer);
        map.addControl(control);
        control.setFeature(feature);
        
        var bottomLeft = new OpenLayers.LonLat(-2, -2);
        control.handles[0].move(bottomLeft);
        t.geom_eq(control.handles[0].geometry, new OpenLayers.Geometry.Point(-2, -2), "bottom left handle at -2,-2");
        t.geom_eq(control.handles[1].geometry, new OpenLayers.Geometry.Point(0, -2), "bottom handle at 0,-2");
        t.geom_eq(control.handles[2].geometry, new OpenLayers.Geometry.Point(2, -2), "bottom right handle at 2,-2");
        t.geom_eq(control.handles[3].geometry, new OpenLayers.Geometry.Point(2, 0), "right handle at 2,0");
        t.geom_eq(control.handles[4].geometry, new OpenLayers.Geometry.Point(2, 2), "top right handle at 2,2");
        t.geom_eq(control.handles[5].geometry, new OpenLayers.Geometry.Point(0, 2), "top handle at 0,2");
        t.geom_eq(control.handles[6].geometry, new OpenLayers.Geometry.Point(-2, 2), "top left handle at -2,2");
        t.geom_eq(control.handles[7].geometry, new OpenLayers.Geometry.Point(-2, 0), "left handle at -2,0");
        
        control.irregular = true;
        
        var bottomLeft = new OpenLayers.LonLat(-3, -3);
        control.handles[0].move(bottomLeft);
        t.geom_eq(control.handles[0].geometry, new OpenLayers.Geometry.Point(-3, -3), "bottom left handle at -3,-3");
        t.geom_eq(control.handles[1].geometry, new OpenLayers.Geometry.Point(-0.5, -3), "bottom handle at 0,-3");
        t.geom_eq(control.handles[2].geometry, new OpenLayers.Geometry.Point(2, -3), "bottom right handle at 2,-3");
        t.geom_eq(control.handles[3].geometry, new OpenLayers.Geometry.Point(2, -0.5), "right handle at 2,0");
        t.geom_eq(control.handles[4].geometry, new OpenLayers.Geometry.Point(2, 2), "top right handle at 2,2");
        t.geom_eq(control.handles[5].geometry, new OpenLayers.Geometry.Point(-0.5, 2), "top handle at 0,2");
        t.geom_eq(control.handles[6].geometry, new OpenLayers.Geometry.Point(-3, 2), "top left handle at -3,2");
        t.geom_eq(control.handles[7].geometry, new OpenLayers.Geometry.Point(-3, -0.5), "left handle at -3,0");
    }

    </script>
</head>
<body>
    <div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>
